package com.qinglei.recoup.system.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qinglei.recoup.system.domain.NurseStatistics;
import com.qinglei.recoup.system.pojo.NurseWorkVO;
import com.qinglei.recoup.system.pojo.PageParam;
import com.qinglei.recoup.system.service.NurseOnlineService;
import com.qinglei.recoup.system.service.NurseStatisticsService;
import com.qinglei.recoup.system.service.UserService;
import com.qinglei.recoup.system.service.impl.NurseOnlineServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 统计任务
 */
@Component
@Slf4j
public class StatisticsTask {
    @Resource
    private UserService userService;

    @Resource
    private NurseStatisticsService nurseStatisticsService;
    @Resource
    private NurseOnlineService nurseOnlineService;

    /**
     * 护工数据统计任务
     */
    public void runNurseStatistics() {
        log.info("runNurseStatistics -护工数据统计任务- run");
        LocalDate yesterday = LocalDate.now().minusDays(1);
        this.runNurseStatisticsByDay(yesterday);
    }

    /**
     * 清理历史数据问题
     */
    public void updateRunNurseStatistics() {
        log.debug("checkBedMonitorTime run");
        nurseOnlineService.updateOnline();
        nurseStatisticsService.remove(new QueryWrapper<>());
        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDate parse = LocalDate.parse("2020-12-02");
        List<LocalDate> ascDateList = getAscDateList(parse, yesterday);
        for (LocalDate localDate : ascDateList) {
            this.runNurseStatisticsByDay(localDate);
        }
    }

    public void runNurseStatisticsByDay(LocalDate day) {

        try {
            PageParam pageParam = new PageParam();
            pageParam.setPageSize(1000L);
            IPage<NurseWorkVO> nurseWorkVOIPage;
            while (true) {
                nurseWorkVOIPage = userService.pageNurse(pageParam, null);
                if (nurseWorkVOIPage == null || CollectionUtils.isEmpty(nurseWorkVOIPage.getRecords())) {
                    break;
                }
                List<NurseWorkVO> records = nurseWorkVOIPage.getRecords();
                if (CollectionUtils.isNotEmpty(records)) {
                    List<NurseStatistics> nurseStatistics = new ArrayList<>(records.size());
                    records.forEach(nurseWorkVO -> {
                        NurseStatistics statisticsByDay = nurseStatisticsService.
                                getStatisticsByDay(nurseWorkVO.getUserId(), day);
                        if (statisticsByDay != null && statisticsByDay.getUserId() != null) {
                            nurseStatistics.add(statisticsByDay);
                        }
                    });
                    if (CollectionUtils.isNotEmpty(nurseStatistics)) {

                        nurseStatisticsService.saveBatch(nurseStatistics);
                    }
                }
                pageParam.setPageNum(pageParam.getPageNum() + 1);
            }
        } catch (Exception e) {
            log.error("runNurseStatistics error", e);
        }
    }


    public List<LocalDate> getAscDateList(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> result = new ArrayList<>();
        if (endDate.compareTo(startDate) < 0) {
            return result;
        }
        while (true) {
            result.add(startDate);
            if (startDate.compareTo(endDate) >= 0) {
                break;
            }
            startDate = startDate.plusDays(1);
        }
        return result;
    }

}
